{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "484b0874",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.metrics import r2_score\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9664573c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "from tensorflow.keras import Sequential\n",
    "from tensorflow.keras.layers import Dense, BatchNormalization, LeakyReLU\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.regularizers import l2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "919b6d04",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"gld_price_data.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "569f2287",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>SPX</th>\n",
       "      <th>GLD</th>\n",
       "      <th>USO</th>\n",
       "      <th>SLV</th>\n",
       "      <th>EUR/USD</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>01/02/08</td>\n",
       "      <td>1447.160034</td>\n",
       "      <td>84.860001</td>\n",
       "      <td>78.470001</td>\n",
       "      <td>15.180</td>\n",
       "      <td>1.471692</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>01/03/08</td>\n",
       "      <td>1447.160034</td>\n",
       "      <td>85.570000</td>\n",
       "      <td>78.370003</td>\n",
       "      <td>15.285</td>\n",
       "      <td>1.474491</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>01/04/08</td>\n",
       "      <td>1411.630005</td>\n",
       "      <td>85.129997</td>\n",
       "      <td>77.309998</td>\n",
       "      <td>15.167</td>\n",
       "      <td>1.475492</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>01/07/08</td>\n",
       "      <td>1416.180054</td>\n",
       "      <td>84.769997</td>\n",
       "      <td>75.500000</td>\n",
       "      <td>15.053</td>\n",
       "      <td>1.468299</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>01/08/08</td>\n",
       "      <td>1390.189941</td>\n",
       "      <td>86.779999</td>\n",
       "      <td>76.059998</td>\n",
       "      <td>15.590</td>\n",
       "      <td>1.557099</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Date          SPX        GLD        USO     SLV   EUR/USD\n",
       "0  01/02/08  1447.160034  84.860001  78.470001  15.180  1.471692\n",
       "1  01/03/08  1447.160034  85.570000  78.370003  15.285  1.474491\n",
       "2  01/04/08  1411.630005  85.129997  77.309998  15.167  1.475492\n",
       "3  01/07/08  1416.180054  84.769997  75.500000  15.053  1.468299\n",
       "4  01/08/08  1390.189941  86.779999  76.059998  15.590  1.557099"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "966876c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df[['SPX','USO','SLV','EUR/USD']]\n",
    "y = df['GLD']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c07a4522",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Split the data into training and testing sets\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7455e5f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "scaler=MinMaxScaler()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6b7b2010",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_scaled = scaler.fit_transform(X_train)\n",
    "X_test_scaled = scaler.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "48246523",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = Sequential()\n",
    "\n",
    "model.add(Dense(32, activation='relu', input_dim=4))\n",
    "model.add(BatchNormalization())\n",
    "model.add(Dense(16, activation='relu'))\n",
    "model.add(BatchNormalization())\n",
    "model.add(Dense(8, activation='relu'))\n",
    "model.add(Dense(1, activation='linear'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fe13061e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compile the model with a lower learning rate and L2 regularization\n",
    "optimizer = Adam(learning_rate=0.001)\n",
    "model.compile(loss=\"mean_squared_error\", optimizer=optimizer)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "373c0c23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/100\n",
      "52/52 [==============================] - 1s 5ms/step - loss: 15304.8730 - val_loss: 15708.4053\n",
      "Epoch 2/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 14771.0381 - val_loss: 15542.6865\n",
      "Epoch 3/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 14129.7539 - val_loss: 15140.6328\n",
      "Epoch 4/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 13294.3955 - val_loss: 14458.8604\n",
      "Epoch 5/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 12270.9502 - val_loss: 13607.0762\n",
      "Epoch 6/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 11040.0410 - val_loss: 12287.5254\n",
      "Epoch 7/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 9585.3516 - val_loss: 10525.4854\n",
      "Epoch 8/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 8010.3799 - val_loss: 8836.4229\n",
      "Epoch 9/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 6288.5825 - val_loss: 6929.2671\n",
      "Epoch 10/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 4717.1387 - val_loss: 4874.6914\n",
      "Epoch 11/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 3305.2129 - val_loss: 3241.6379\n",
      "Epoch 12/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 2272.3542 - val_loss: 1957.0859\n",
      "Epoch 13/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 1402.4030 - val_loss: 1069.7037\n",
      "Epoch 14/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 987.7748 - val_loss: 525.3793\n",
      "Epoch 15/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 730.0342 - val_loss: 325.7230\n",
      "Epoch 16/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 450.3367 - val_loss: 187.5773\n",
      "Epoch 17/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 370.8854 - val_loss: 154.5080\n",
      "Epoch 18/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 414.2657 - val_loss: 94.6718\n",
      "Epoch 19/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 376.8248 - val_loss: 49.1882\n",
      "Epoch 20/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 309.6599 - val_loss: 58.1425\n",
      "Epoch 21/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 389.4264 - val_loss: 123.5352\n",
      "Epoch 22/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 307.7685 - val_loss: 47.8060\n",
      "Epoch 23/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 228.6202 - val_loss: 50.7492\n",
      "Epoch 24/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 245.1460 - val_loss: 56.2384\n",
      "Epoch 25/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 178.6969 - val_loss: 77.5807\n",
      "Epoch 26/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 162.8485 - val_loss: 92.8994\n",
      "Epoch 27/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 188.3134 - val_loss: 80.0995\n",
      "Epoch 28/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 220.8654 - val_loss: 44.0109\n",
      "Epoch 29/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 197.3504 - val_loss: 66.1544\n",
      "Epoch 30/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 236.7831 - val_loss: 99.2187\n",
      "Epoch 31/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 176.2085 - val_loss: 85.3900\n",
      "Epoch 32/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 175.3212 - val_loss: 155.0774\n",
      "Epoch 33/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 116.0559 - val_loss: 137.2745\n",
      "Epoch 34/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 128.9993 - val_loss: 181.3791\n",
      "Epoch 35/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 208.2269 - val_loss: 48.8132\n",
      "Epoch 36/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 137.9986 - val_loss: 189.3881\n",
      "Epoch 37/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 163.9003 - val_loss: 57.6553\n",
      "Epoch 38/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 148.5900 - val_loss: 52.2488\n",
      "Epoch 39/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 119.2904 - val_loss: 47.5964\n",
      "Epoch 40/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 125.1296 - val_loss: 329.1179\n",
      "Epoch 41/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 93.7821 - val_loss: 70.6979\n",
      "Epoch 42/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 125.1611 - val_loss: 142.3922\n",
      "Epoch 43/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 119.5163 - val_loss: 64.7298\n",
      "Epoch 44/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 119.9083 - val_loss: 214.3814\n",
      "Epoch 45/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 170.1375 - val_loss: 215.4284\n",
      "Epoch 46/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 112.2194 - val_loss: 167.3709\n",
      "Epoch 47/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 80.0805 - val_loss: 282.3225\n",
      "Epoch 48/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 109.6539 - val_loss: 98.2927\n",
      "Epoch 49/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 92.5042 - val_loss: 167.2021\n",
      "Epoch 50/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 90.2667 - val_loss: 105.5259\n",
      "Epoch 51/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 108.0885 - val_loss: 175.4377\n",
      "Epoch 52/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 98.9612 - val_loss: 105.4652\n",
      "Epoch 53/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 88.3914 - val_loss: 178.7829\n",
      "Epoch 54/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 94.3760 - val_loss: 63.4042\n",
      "Epoch 55/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 75.2459 - val_loss: 255.6528\n",
      "Epoch 56/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 82.6317 - val_loss: 108.2983\n",
      "Epoch 57/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 102.6565 - val_loss: 67.3034\n",
      "Epoch 58/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 95.1825 - val_loss: 86.6359\n",
      "Epoch 59/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 81.8627 - val_loss: 182.8544\n",
      "Epoch 60/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 94.4717 - val_loss: 130.0062\n",
      "Epoch 61/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 75.6448 - val_loss: 145.8914\n",
      "Epoch 62/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 75.3606 - val_loss: 47.7060\n",
      "Epoch 63/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 74.8475 - val_loss: 119.0299\n",
      "Epoch 64/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 84.7068 - val_loss: 131.0818\n",
      "Epoch 65/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 71.4457 - val_loss: 45.7404\n",
      "Epoch 66/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 96.1561 - val_loss: 104.3360\n",
      "Epoch 67/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 83.9160 - val_loss: 58.0162\n",
      "Epoch 68/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 75.0167 - val_loss: 62.1173\n",
      "Epoch 69/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 90.9415 - val_loss: 54.2195\n",
      "Epoch 70/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 71.6245 - val_loss: 62.3442\n",
      "Epoch 71/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 83.0387 - val_loss: 209.2570\n",
      "Epoch 72/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 75.0388 - val_loss: 135.1255\n",
      "Epoch 73/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 73.3539 - val_loss: 178.8017\n",
      "Epoch 74/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 80.8245 - val_loss: 60.4232\n",
      "Epoch 75/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 88.8882 - val_loss: 85.6680\n",
      "Epoch 76/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 70.0898 - val_loss: 50.6072\n",
      "Epoch 77/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 75.0763 - val_loss: 40.8105\n",
      "Epoch 78/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 69.6293 - val_loss: 237.3010\n",
      "Epoch 79/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 79.8115 - val_loss: 88.8350\n",
      "Epoch 80/100\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "52/52 [==============================] - 0s 2ms/step - loss: 80.7484 - val_loss: 57.9937\n",
      "Epoch 81/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 70.4915 - val_loss: 31.3283\n",
      "Epoch 82/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 63.0157 - val_loss: 50.0905\n",
      "Epoch 83/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 64.6075 - val_loss: 37.3694\n",
      "Epoch 84/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 64.7124 - val_loss: 36.9142\n",
      "Epoch 85/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 58.0323 - val_loss: 37.0538\n",
      "Epoch 86/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 73.4507 - val_loss: 45.0116\n",
      "Epoch 87/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 57.4454 - val_loss: 65.2783\n",
      "Epoch 88/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 65.5154 - val_loss: 37.8791\n",
      "Epoch 89/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 65.9017 - val_loss: 32.1330\n",
      "Epoch 90/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 57.5096 - val_loss: 41.4032\n",
      "Epoch 91/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 58.3772 - val_loss: 64.2754\n",
      "Epoch 92/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 60.9921 - val_loss: 49.7985\n",
      "Epoch 93/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 55.2542 - val_loss: 39.2479\n",
      "Epoch 94/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 54.2289 - val_loss: 86.8420\n",
      "Epoch 95/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 60.0738 - val_loss: 63.5683\n",
      "Epoch 96/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 69.5935 - val_loss: 32.8206\n",
      "Epoch 97/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 52.2506 - val_loss: 67.2782\n",
      "Epoch 98/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 53.4356 - val_loss: 43.5972\n",
      "Epoch 99/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 51.5107 - val_loss: 57.3120\n",
      "Epoch 100/100\n",
      "52/52 [==============================] - 0s 2ms/step - loss: 57.2784 - val_loss: 36.8163\n"
     ]
    }
   ],
   "source": [
    "# Train the model\n",
    "history = model.fit(X_train_scaled, y_train, epochs=100, batch_size=32, validation_split=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c4b720ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15/15 [==============================] - 0s 1ms/step\n"
     ]
    }
   ],
   "source": [
    "# Make predictions\n",
    "y_pred = model.predict(X_test_scaled)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "eac5f7d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R-squared score: 0.9471963282577486\n"
     ]
    }
   ],
   "source": [
    "# Calculate R-squared score\n",
    "r2 = r2_score(y_test, y_pred)\n",
    "print(\"R-squared score:\", r2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53250bf9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
